package com.example.cloud.service.basic.info.impl;

import com.example.cloud.dao.basic.info.HomeDiscountProjectDetailsRepository;
import com.example.cloud.dao.mongdb.basic.info.MongoHomeDiscountProjectDetailsRepository;
import com.example.cloud.model.basic.HomeDiscountProjectDetails;
import com.example.cloud.service.basic.info.HomeDiscountProjectDetailsService;
import com.example.cloud.utils.BusinessUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by wangtao on 2018/6/6.
 */
@Service
public class HomeDiscountProjectDetailsServiceImpl implements HomeDiscountProjectDetailsService {

    private static final Logger logger = Logger.getLogger(HomeDiscountProjectDetailsServiceImpl.class);

    @PersistenceContext
    EntityManager em;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    private HomeDiscountProjectDetailsRepository homeDiscountProjectDetailsRepository;

    @Autowired
    private MongoHomeDiscountProjectDetailsRepository mongoHomeDiscountProjectDetailsRepository;

    @Override
    public List<HomeDiscountProjectDetails> findByHomeIdFromCache(Long homeId) {
        return null;
    }

    @Override
    @Transactional
    public HomeDiscountProjectDetails doSave(HomeDiscountProjectDetails record) {
        // 物理保存
        BusinessUtil.appendRecord(record);
        record = homeDiscountProjectDetailsRepository.save(record);
        // 同步缓存
        mongoHomeDiscountProjectDetailsRepository.save(record);
        return record;
    }

    @Override
    @Transactional
    public void batchInsert(List<HomeDiscountProjectDetails> records) {
        for (int i = 0; i < records.size(); i++) {
            HomeDiscountProjectDetails record = records.get(i);
            BusinessUtil.appendRecord(record);
            em.merge(record);
            if (i % 20 == 0) {
                em.flush();
                em.clear();
            }
        }
        mongoHomeDiscountProjectDetailsRepository.save(records);
    }

    @Override
    @Transactional
    public void doDelete(List<Long> ids) {
        // 逻辑删除
        StringBuilder sql = new StringBuilder("update basic_home_discount_project_details set delete_flag = 1 where id in(:ids)");
        Query query = em.createNativeQuery(sql.toString());
        query.setParameter("ids", ids);
        query.executeUpdate();
        // 删除缓存
        mongoHomeDiscountProjectDetailsRepository.deleteByIdIn(ids);
    }

    @Override
    public void loadCache() {
        List<HomeDiscountProjectDetails> list = homeDiscountProjectDetailsRepository.findAll();
        if(!list.isEmpty()){
            mongoHomeDiscountProjectDetailsRepository.save(list);
            logger.info("*******************家庭病房折扣方案项目明细类已全部同步缓存*******************");
        }
    }
}
